<?php
/**
 * Created by xLong.
 * User: CYT
 * Date: 2018/3/26
 * Time: 15:48
 */

namespace app\cls\tom;


class VideoCurl
{
    private static $appid = "d964877cff0c0c73f8345f7664fb0889";
    private static $app_secret = "ZDk2NDg3N2NmZjBjMGM3M2Y4MzQ1Zjc2NjRmYjA4ODnllYrmiYvliqjpmIDmkpLml6bl";
    private static $api = "https://api-live.foundao.com/toutiao/";

    /**
     * 功能描述：发送请求 并带上签名
     * @param $url
     * @param null $data
     * @param null $header
     * @return mixed
     */
    public static function curlSendDataBySign($url, $data = null, $header = null)
    {
        // $data['time'] = time();
        // $data['sign'] = self::getSign($data, "newscctv");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $response = curl_exec($ch);
        return $response;
    }

    /**
     * 工程状态查询
     * @param $jobId
     * @return mixed
     */
    public static function getJobStatus($jobId)
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['job_id'] = $jobId;
        $responseData = self::curlSendDataBySign(self::$api . "getJobStatus.php", $curlData);
        return $responseData;
    }

    /**
     * 添加视频水印
     * @param $videoUrl
     * @param $ext
     * @param $waterJson
     * @param $outputUrl
     * @param $attach
     * @return mixed
     */
    public static function addWatermark($videoUrl, $ext, $waterJson, $outputUrl = '',$attach=null,$water = [])
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['video_url'] = $videoUrl;
        $curlData['ext'] = $ext;
        $curlData['output_url'] = $outputUrl;
        $curlData['attach'] = $attach;
        if($water){
            $waters = json_decode($waterJson,true);
            $waters[] = $water;
            $curlData["water"] = json_encode($waters);
        }else{
            $curlData["water"] = $waterJson;
        }
        $responseData = self::curlSendDataBySign(self::$api . "submitWatermarkVideoJob.php", $curlData);
        return $responseData;
    }

    /**
     * 添加视频水印 - 状态查询
     * @param $jobId
     * @return mixed
     */
    public static function addWatermarkStatus($jobId)
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['job_id'] = $jobId;
        $responseData = self::curlSendDataBySign(self::$api . "getWatermarkVideoJobStatus.php", $curlData);
        return $responseData;
    }

    /**
     * 视频合并
     * @param $ext
     * @param $videos
     * @param $attach
     * @return mixed
     */
    public static function mergeVideo($ext, $exportWidth,$exportHeight,$videos,$outputUrl = null,$attach=null,$water=[])
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['ext'] = $ext;
        $curlData['videos'] = json_encode($videos);
        $curlData['output_url'] = $outputUrl;
        $curlData['width'] = $exportWidth;
        $curlData['height'] = $exportHeight;
        $curlData['attach'] = $attach;
        if($water){
            $curlData["water"] = json_encode([$water]);
        }
        Tools::writeLog(["msg"=>"合并视频","detail"=>$curlData,"key"=>"merge"]);
        $responseData = self::curlSendDataBySign(self::$api . "submitJoinVideoJob.php", $curlData);
        return $responseData;
    }

    /**
     * 视频合并 - 状态查询
     * @param $jobId
     * @return mixed
     */
    public static function mergeVideoStatus($jobId)
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['job_id'] = $jobId;
        $responseData = self::curlSendDataBySign(self::$api . "getJoinVideoJobStatus.php", $curlData);
        return $responseData;
    }

    /**
     * 查询视频元信息
     * @param $videoUrl
     * @return mixed
     */
    public static function getMediaInfo($videoUrl)
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['video_url'] = $videoUrl;
        $responseData = self::curlSendDataBySign(self::$api . "getMediaInfo.php", $curlData);
        return $responseData;
    }

    /**
     * 获取视频帧缩略图
     * @param $videoUrl
     * @return mixed
     */
    public static function getVideoCover($videoUrl, $outputUrl = null)
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['video_url'] = $videoUrl;
        if (!empty($outputUrl)) {
            $curlData['output_url'] = $outputUrl;
        }
        $responseData = self::curlSendDataBySign(self::$api . "getVideoCover.php", $curlData);
        return $responseData;
    }

    /**
     * 提交音视频切片任务
     * @param $videoUrl string 资源地址
     * @param $ext string 目标资源后缀
     * @param $time array 切片起始时间
     * @param $outputurl string 目标地址
     * @param $defaultWater string 水印地址
     * @return mixed
     */
    public static function submitSliceVideoJob($videoUrl, $ext,array $time, $outputurl = null,$attach=null,$defaultWater=[])
    {
        $curlData = array();
        $curlData['app_id'] = self::$appid;
        $curlData['app_secret'] = self::$app_secret;
        $curlData['video_url'] = $videoUrl;
        $curlData['ext'] = $ext;
        $curlData['start_time'] = $time[0]["start_time"];
        $curlData['duration'] = $time[0]["duration"];
        $curlData['attach'] = $attach;
        if($defaultWater){
            $curlData["water"] = json_encode([$defaultWater]);
        }
        if (!empty($outputurl)) {
            $curlData['output_url'] = $outputurl;
        }
        Tools::writeLog(["msg"=>"裁剪","detail"=>$curlData]);
        $responseData = self::curlSendDataBySign(self::$api . "submitSliceVideoJob.php", $curlData);
        return $responseData;
    }

}